﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BOH
{
    public class UserTemplate:ITemplate
    {
        private DataControlRowType templateType;
        private string colName;
        private string colHiddenName;
        private string controlType;
        private int colIndex;
        private int colIndex2;

        public UserTemplate(DataControlRowType rtype, string controlStr, string colN,string colNH)
        {
            templateType = rtype;
            colName = colN;
            controlType = controlStr;
            colHiddenName = colNH;
        }

        public UserTemplate(DataControlRowType rtype, string controlStr, int colI, int coli2)
        {
            templateType = rtype;
            colIndex = colI;
            controlType = controlStr;
            colIndex2 = coli2;
        }

        public UserTemplate(string HeaderText)
        {
            templateType = DataControlRowType.Header;
            colName = HeaderText;
            controlType = "lab";
            
        }

        #region ITemplate Members

        public void InstantiateIn(Control container)
        {
            switch (templateType)
            { 
                case DataControlRowType.DataRow:
                    switch (controlType.ToLower())
                    { 
                        //case "hidden":
                            
                        case "htextbox":
                            HiddenField hidden = new HiddenField();
                            hidden.DataBinding += new EventHandler(hidden_DataBinding);
                            container.Controls.Add(hidden);

                            TextBox txt = new TextBox();
                            txt.Width = 50;
                            txt.DataBinding += new EventHandler(TextBoxDataBinding);
                            txt.Attributes.Add("onblur", "SetScoreAndCourse(this)");
                            txt.Attributes.Add("onfocus", "SetOldValue(this)");
                            container.Controls.Add(txt);
                            break;
                        default :
                            Label lab = new Label();
                            lab.DataBinding += new EventHandler(labDataBinding);
                            container.Controls.Add(lab);
                            break;
                    }
                    break;
                case DataControlRowType.Header:
                    {
                        if (controlType.ToLower() == "lab")
                        {
                            Label labHead = new Label();
                            labHead.DataBinding += new EventHandler(labHead_DataBinding);
                            container.Controls.Add(labHead);
                        }
                    }

                    break;
                default:
                    break;
            }
        }

        void labHead_DataBinding(object sender, EventArgs e)
        {
            Label lab = (Label)sender;
            GridViewRow row = (GridViewRow)lab.NamingContainer;
            lab.Text = colName;
        }
        #endregion

        void hidden_DataBinding(object sender, EventArgs e)
        {
            HiddenField hidden = (HiddenField)sender;
            GridViewRow row = (GridViewRow)hidden.NamingContainer;
            if(colIndex2!=-1)
                hidden.Value = ((DataRowView)row.DataItem).Row[colIndex2].ToString() + ":" + ((DataRowView)row.DataItem).Row[0].ToString();
        }

        public void TextBoxDataBinding(Object sender, EventArgs e)
        {
            TextBox txt = (TextBox)sender;
            GridViewRow row = (GridViewRow)txt.NamingContainer;
            txt.Text = ((DataRowView)row.DataItem).Row[colIndex].ToString();
        }

        public void labDataBinding(Object sender, EventArgs e)
        {
            Label lab = (Label)sender;
            GridViewRow row = (GridViewRow)lab.NamingContainer;
            lab.Text = ((DataRowView)row.DataItem).Row[colIndex].ToString();
        }
    }
}
